﻿using Autofac;
using MovieDB.Autofac;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MovieDB.Test.Common
{
    public class BaseTest<TSerivce>
    {
        protected ContainerBuilder ContainerBuilder;
        protected TSerivce ServiceToTest;

        public BaseTest()
        {
            //setup containerbuilder
            ContainerBuilder = new ContainerBuilder();
        }

        public virtual void Init()
        {
           //register service under test
            ContainerBuilder.RegisterType<TSerivce>();

            //build and setup the container
            IContainer container = ContainerBuilder.Build();
            AutofacResolver.SetResolver(container);

            ServiceToTest = AutofacResolver.Resolve<TSerivce>();
        }

        protected void RegisterMock<T>(T mockObject) where T : class
        {
            ContainerBuilder.RegisterInstance(mockObject).As<T>();
        }

        [TestCleanup]
        public virtual void Cleanup()
        {
            ContainerBuilder = new ContainerBuilder();
        }
    }
}